package com.naizhuai.ui.home;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.View;

import com.naizhuai.R;
import com.naizhuai.ui.home.Chart.ComPanyInfo;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import java.util.ArrayList;

import butterknife.Unbinder;

/**
 * @author：lichenxi
 * @date 2018/1/25 21
 * email：525603977@qq.com
 * Fighting
 */
public class ChartDiaLog extends AlertDialog {

    BarChart mChart;

    private Context context;
    private ComPanyInfo comPanyInfo;
    private static int LINE_SPACE=15;
    public void setComPanyInfo(ComPanyInfo comPanyInfo) {
        this.comPanyInfo = comPanyInfo;
    }

    public ChartDiaLog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public ChartDiaLog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public ChartDiaLog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = View.inflate(context, R.layout.chart_dialog, null);
        setContentView(view);
        mChart=view.findViewById(R.id.companyDetiail);
        InitChart();
        InitData();
    }

    private void InitData() {

        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
        yVals1.add(new BarEntry(0, comPanyInfo.noslove));
        yVals1.add(new BarEntry(1, comPanyInfo.sloved));
        yVals1.add(new BarEntry(2, comPanyInfo.sloving));

        BarDataSet set1;

        if (mChart.getData() != null &&
                mChart.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet) mChart.getData().getDataSetByIndex(0);
            set1.setValues(yVals1);
            mChart.getData().notifyDataChanged();
            mChart.notifyDataSetChanged();
        } else {
            set1 = new BarDataSet(yVals1, context.getString(R.string.comPany_Detiail));

            set1.setDrawIcons(false);

            set1.setColors(ColorTemplate.MATERIAL_COLORS1);

            ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
            dataSets.add(set1);

            BarData data = new BarData(dataSets);
            data.setValueTextSize(10f);
            data.setBarWidth(0.9f);
            mChart.setData(data);
        }
    }

    private void InitChart() {

        mChart.setDrawBarShadow(false);
        mChart.setDrawValueAboveBar(true);
        mChart.getDescription().setEnabled(false);
        mChart.setDoubleTapToZoomEnabled(false);
        // if more than 60 entries are displayed in the chart, no values will be
        // drawn
        mChart.setMaxVisibleValueCount(60);
        mChart.setVisibleYRangeMaximum(100, YAxis.AxisDependency.RIGHT);
        // scaling can now only be done on x- and y-axis separately
        mChart.setPinchZoom(false);

        mChart.setDrawGridBackground(false);

        XAxis xl = mChart.getXAxis();
        xl.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                int index= (int) value;
                return ComPanyInfo.ProJect_Progress[index] ;
            }
        });
        XAxis xAxis = mChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f);
        xAxis.setLabelCount(7);


        YAxis yl = mChart.getAxisLeft();
        yl.setDrawAxisLine(true);
        yl.setDrawGridLines(true);
        yl.setAxisMinimum(0f); // this replaces setStartAtZero(true)
        yl.setDrawLabels(true);

//        YAxis yr = companyQuestionChart.getAxisRight();
//        yr.setTypeface(mTfLight);
//        yr.setDrawAxisLine(true);
//        yr.setDrawGridLines(false);
//        yr.setAxisMinimum(0f); // this replaces setStartAtZero(true)

        mChart.setFitBars(true);
        mChart.animateY(2000);
        Legend l = mChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        l.setForm(Legend.LegendForm.SQUARE);
        l.setFormSize(9f);
        l.setTextSize(11f);
        l.setXEntrySpace(4f);
    }

}
